package com.pointi.emoEngine;

public class DSP_MFCC
{
	DSP_MFCC() {
	}

	//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	public double DCTMat[][] =
		{
			{0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356, 0.213200716356},
			{0.300743128228, 0.294620865409, 0.282500971386, 0.264630172252, 0.241372265941, 0.213200716356, 0.180689015005, 0.144499006373, 0.105367414673, 0.064090846264, 0.021509573047, -0.021509573047, -0.064090846264, -0.105367414673, -0.144499006373, -0.180689015005, -0.213200716356, -0.241372265941, -0.264630172252, -0.282500971386, -0.294620865409, -0.300743128228},
			{0.298442393833, 0.274264365990, 0.227867070328, 0.163009339805, 0.084945562025, 0.000000000000, -0.084945562025, -0.163009339805, -0.227867070328, -0.274264365990, -0.298442393833, -0.298442393833, -0.274264365990, -0.227867070328, -0.163009339805, -0.084945562025, -0.000000000000, 0.084945562025, 0.163009339805, 0.227867070328, 0.274264365990, 0.298442393833},
			{0.294620865409, 0.241372265941, 0.144499006373, 0.021509573047, -0.105367414673, -0.213200716356, -0.282500971386, -0.300743128228, -0.264630172252, -0.180689015005, -0.064090846264, 0.064090846264, 0.180689015005, 0.264630172252, 0.300743128228, 0.282500971386, 0.213200716356, 0.105367414673, -0.021509573047, -0.144499006373, -0.241372265941, -0.294620865409},
			{0.289298016587, 0.197447940403, 0.042909538241, -0.125252339128, -0.253647483815, -0.301511344578, -0.253647483815, -0.125252339128, 0.042909538241, 0.197447940403, 0.289298016587, 0.289298016587, 0.197447940403, 0.042909538241, -0.125252339128, -0.253647483815, -0.301511344578, -0.253647483815, -0.125252339128, 0.042909538241, 0.197447940403, 0.289298016587},
			{0.282500971386, 0.144499006373, -0.064090846264, -0.241372265941, -0.300743128228, -0.213200716356, -0.021509573047, 0.180689015005, 0.294620865409, 0.264630172252, 0.105367414673, -0.105367414673, -0.264630172252, -0.294620865409, -0.180689015005, 0.021509573047, 0.213200716356, 0.300743128228, 0.241372265941, 0.064090846264, -0.144499006373, -0.282500971386},
			{0.274264365990, 0.084945562025, -0.163009339805, -0.298442393833, -0.227867070328, -0.000000000000, 0.227867070328, 0.298442393833, 0.163009339805, -0.084945562025, -0.274264365990, -0.274264365990, -0.084945562025, 0.163009339805, 0.298442393833, 0.227867070328, -0.000000000000, -0.227867070328, -0.298442393833, -0.163009339805, 0.084945562025, 0.274264365990},
			{0.264630172252, 0.021509573047, -0.241372265941, -0.282500971386, -0.064090846264, 0.213200716356, 0.294620865409, 0.105367414673, -0.180689015005, -0.300743128228, -0.144499006373, 0.144499006373, 0.300743128228, 0.180689015005, -0.105367414673, -0.294620865409, -0.213200716356, 0.064090846264, 0.282500971386, 0.241372265941, -0.021509573047, -0.264630172252},
			{0.253647483815, -0.042909538241, -0.289298016587, -0.197447940403, 0.125252339128, 0.301511344578, 0.125252339128, -0.197447940403, -0.289298016587, -0.042909538241, 0.253647483815, 0.253647483815, -0.042909538241, -0.289298016587, -0.197447940403, 0.125252339128, 0.301511344578, 0.125252339128, -0.197447940403, -0.289298016587, -0.042909538241, 0.253647483815},
			{0.241372265941, -0.105367414673, -0.300743128228, -0.064090846264, 0.264630172252, 0.213200716356, -0.144499006373, -0.294620865409, -0.021509573047, 0.282500971386, 0.180689015005, -0.180689015005, -0.282500971386, 0.021509573047, 0.294620865409, 0.144499006373, -0.213200716356, -0.264630172252, 0.064090846264, 0.300743128228, 0.105367414673, -0.241372265941},
			{0.227867070328, -0.163009339805, -0.274264365990, 0.084945562025, 0.298442393833, -0.000000000000, -0.298442393833, -0.084945562025, 0.274264365990, 0.163009339805, -0.227867070328, -0.227867070328, 0.163009339805, 0.274264365990, -0.084945562025, -0.298442393833, -0.000000000000, 0.298442393833, 0.084945562025, -0.274264365990, -0.163009339805, 0.227867070328},
			{0.213200716356, -0.213200716356, -0.213200716356, 0.213200716356, 0.213200716356, -0.213200716356, -0.213200716356, 0.213200716356, 0.213200716356, -0.213200716356, -0.213200716356, 0.213200716356, 0.213200716356, -0.213200716356, -0.213200716356, 0.213200716356, 0.213200716356, -0.213200716356, -0.213200716356, 0.213200716356, 0.213200716356, -0.213200716356}
	} ; // [12][22]
	//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	public double FilterWeight1[] = {5.000000000000, 8.000000000000, 0.005156257501, 0.012187507502, 0.010781242499, 0.003749992498};
	public double FilterWeight2[] = {7.000000000000, 10.000000000000, 0.004218757502, 0.011250007504, 0.011718742498, 0.004687492496};
	public double FilterWeight3[] = {9.000000000000, 12.000000000000, 0.003281257504, 0.010312507505, 0.012656242497, 0.005624992495};
	public double FilterWeight4[] = {11.000000000000, 14.000000000000, 0.002343757505, 0.009375007506, 0.013593742495, 0.006562492494};
	public double FilterWeight5[] = {13.000000000000, 17.000000000000, 0.001406257506, 0.008437507508, 0.014531242494, 0.007499992493, 0.000468742491};
	public double FilterWeight6[] = {15.000000000000, 19.000000000000, 0.000468757508, 0.007500007509, 0.014531257510, 0.008437492491, 0.001406242490};
	public double FilterWeight7[] = {18.000000000000, 21.000000000000, 0.006562507510, 0.013593757512, 0.009374992490, 0.002343742488};
	public double FilterWeight8[] = {20.000000000000, 23.000000000000, 0.005625007512, 0.012656257513, 0.010312492489, 0.003281242487};
	public double FilterWeight9[] = {22.000000000000, 25.000000000000, 0.004687507513, 0.011718757514, 0.011249992487, 0.004218742486};
	public double FilterWeight10[] = {24.000000000000, 27.000000000000, 0.003750007514, 0.010781257516, 0.012187492486, 0.005156242485};
	public double FilterWeight11[] = {26.000000000000, 29.000000000000, 0.002812507516, 0.009843757517, 0.013124992485, 0.006093742483};
	public double FilterWeight12[] = {28.000000000000, 31.000000000000, 0.001878403516, 0.008922388463, 0.014084554024, 0.007015006757};
	public double FilterWeight13[] = {30.000000000000, 34.000000000000, 0.000911875005, 0.007750882701, 0.014487872120, 0.008103259239, 0.001718646358};
	public double FilterWeight14[] = {32.000000000000, 36.000000000000, 0.000045983672, 0.006006392471, 0.011966801269, 0.009504722413, 0.003940332886};
	public double FilterWeight15[] = {35.000000000000, 39.000000000000, 0.003796348303, 0.008991030723, 0.011254145388, 0.006404606131, 0.001555066874};
	public double FilterWeight16[] = {37.000000000000, 42.000000000000, 0.001321373533, 0.005848701501, 0.010376029468, 0.008956539593, 0.004730014672, 0.000503489751};
	public double FilterWeight17[] = {40.000000000000, 45.000000000000, 0.002680466003, 0.006626173722, 0.010571881440, 0.007797177086, 0.004113628617, 0.000430080148};
	public double FilterWeight18[] = {43.000000000000, 48.000000000000, 0.003029155519, 0.006467963026, 0.009906770533, 0.007472776204, 0.004262448670, 0.001052121137};
	public double FilterWeight19[] = {46.000000000000, 51.000000000000, 0.002647104062, 0.005644132203, 0.008641160345, 0.007742432560, 0.004944531851, 0.002146631143};
	public double FilterWeight20[] = {49.000000000000, 55.000000000000, 0.001755971276, 0.004367974903, 0.006979978530, 0.008416382236, 0.005977924385, 0.003539466535, 0.001101008685};
	public double FilterWeight21[] = {52.000000000000, 59.000000000000, 0.000529889381, 0.002806332118, 0.005082774856, 0.007359217594, 0.007221444749, 0.005096252576, 0.002971060403, 0.000845868230};
	public double FilterWeight22[] = {56.000000000000, 63.000000000000, 0.001088182447, 0.003072173390, 0.005056164332, 0.007040155275, 0.006714851870, 0.004862680518, 0.003010509166, 0.001158337814};
	//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	public void GetMFCC(double [] spectrum, double [] ceps)
	{
		double sum = 0.0 ;
		int m, n ;
		double [] EarMag = new double [22] ;

		// FFT Result X Filter Weights
		for (m=2 ; m<=2+(int)(FilterWeight1[1]-FilterWeight1[0]) ; m++)
		{	sum += (FilterWeight1[m] * spectrum[m-2+(int)FilterWeight1[0]]) ;	EarMag[0] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight2[1]-FilterWeight2[0]) ; m++)
		{	sum += (FilterWeight2[m] * spectrum[m-2+(int)FilterWeight2[0]]) ;	EarMag[1] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight3[1]-FilterWeight3[0]) ; m++)
		{	sum += (FilterWeight3[m] * spectrum[m-2+(int)FilterWeight3[0]]) ;	EarMag[2] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight4[1]-FilterWeight4[0]) ; m++)
		{	sum += (FilterWeight4[m] * spectrum[m-2+(int)FilterWeight4[0]]) ;	EarMag[3] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight5[1]-FilterWeight5[0]) ; m++)
		{	sum += (FilterWeight5[m] * spectrum[m-2+(int)FilterWeight5[0]]) ;	EarMag[4] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight6[1]-FilterWeight6[0]) ; m++)
		{	sum += (FilterWeight6[m] * spectrum[m-2+(int)FilterWeight6[0]]) ;	EarMag[5] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight7[1]-FilterWeight7[0]) ; m++)
		{	sum += (FilterWeight7[m] * spectrum[m-2+(int)FilterWeight7[0]]) ;	EarMag[6] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight8[1]-FilterWeight8[0]) ; m++)
		{	sum += (FilterWeight8[m] * spectrum[m-2+(int)FilterWeight8[0]]) ;	EarMag[7] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight9[1]-FilterWeight9[0]) ; m++)
		{	sum += (FilterWeight9[m] * spectrum[m-2+(int)FilterWeight9[0]]) ;	EarMag[8] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight10[1]-FilterWeight10[0]) ; m++)
		{	sum += (FilterWeight10[m] * spectrum[m-2+(int)FilterWeight10[0]]) ;	EarMag[9] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight11[1]-FilterWeight11[0]) ; m++)
		{	sum += (FilterWeight11[m] * spectrum[m-2+(int)FilterWeight11[0]]) ;	EarMag[10] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight12[1]-FilterWeight12[0]) ; m++)
		{	sum += (FilterWeight12[m] * spectrum[m-2+(int)FilterWeight12[0]]) ;	EarMag[11] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight13[1]-FilterWeight13[0]) ; m++)
		{	sum += (FilterWeight13[m] * spectrum[m-2+(int)FilterWeight13[0]]) ;	EarMag[12] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight14[1]-FilterWeight14[0]) ; m++)
		{	sum += (FilterWeight14[m] * spectrum[m-2+(int)FilterWeight14[0]]) ;	EarMag[13] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight15[1]-FilterWeight15[0]) ; m++)
		{	sum += (FilterWeight15[m] * spectrum[m-2+(int)FilterWeight15[0]]) ;	EarMag[14] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight16[1]-FilterWeight16[0]) ; m++)
		{	sum += (FilterWeight16[m] * spectrum[m-2+(int)FilterWeight16[0]]) ;	EarMag[15] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight17[1]-FilterWeight17[0]) ; m++)
		{	sum += (FilterWeight17[m] * spectrum[m-2+(int)FilterWeight17[0]]) ;	EarMag[16] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight18[1]-FilterWeight18[0]) ; m++)
		{	sum += (FilterWeight18[m] * spectrum[m-2+(int)FilterWeight18[0]]) ;	EarMag[17] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight19[1]-FilterWeight19[0]) ; m++)
		{	sum += (FilterWeight19[m] * spectrum[m-2+(int)FilterWeight19[0]]) ;	EarMag[18] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight20[1]-FilterWeight20[0]) ; m++)
		{	sum += (FilterWeight20[m] * spectrum[m-2+(int)FilterWeight20[0]]) ;	EarMag[19] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight21[1]-FilterWeight21[0]) ; m++)
		{	sum += (FilterWeight21[m] * spectrum[m-2+(int)FilterWeight21[0]]) ;	EarMag[20] = (double)Math.log10(sum) ;	}
		sum = 0.0 ;
		for (m=2 ; m<=2+(int)(FilterWeight22[1]-FilterWeight22[0]) ; m++)
		{	sum += (FilterWeight22[m] * spectrum[m-2+(int)FilterWeight22[0]]) ;	EarMag[21] = (double)Math.log10(sum) ;	}
		//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
		// Take the DCT 
		//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
		for (m=0 ; m<12 ; m++)
		{
			sum =0.0 ;
			for (n=0 ; n<22 ; n++)
			{
				sum += (DCTMat[m][n] * EarMag[n]) ;
			}
			ceps[m] = sum ;
		} 		
		//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	}
}
